package com.divine.wn.car.admin.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.divine.wn.car.admin.form.ManagementUserForm;
import com.divine.wn.car.admin.service.BaseDataService;
import com.divine.wn.car.admin.service.MaintainVehicleService;
import com.divine.wn.car.admin.vo.BackUserVO;

public class ManagementUserAction extends BaseAction {
	//添加用户
	public ActionForward toAddUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		managementUserForm.setBackUser(new BackUserVO());
		this.getRoleAndArea( managementUserForm,request);
		return mapping.findForward("toAddUser");
	}
	
	public ActionForward getAreaByRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		baseDataService = (BaseDataService) ac.getBean("baseDataService");
		
		List<HashMap<String,String>> roles = baseDataService.getRoleData();
		request.setAttribute("roles", roles);
		return mapping.findForward("toAddUser");
	}
	
	public ActionForward updateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
		maintainVehicleService.updateUser(managementUserForm.getBackUser());
		this.getRoleAndArea(managementUserForm, request);
		return this.getUser(mapping, managementUserForm, request, response);
	}
	
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
		maintainVehicleService.deleteUser(managementUserForm.getBackUser());
		this.getRoleAndArea(managementUserForm, request);
		return this.getUser(mapping, managementUserForm, request, response);
	}
	
	
	
	public ActionForward toUpdaterUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
		
		HashMap<String,Object> map = new HashMap<String,Object>();
		
		map.put("userID", request.getParameter("backUser.userid"));
		map.put("limit", 1); 
		map.put("skip", 0); 
		List<HashMap<String,Object>> userList=maintainVehicleService.getUser(map);
		HashMap<String,Object> mapUser=userList.get(0);
		
		BackUserVO userVO=new BackUserVO();
		userVO.setUsername(mapUser.get("username").toString());
		userVO.setUsernum(mapUser.get("usernum").toString());
		
		userVO.setAddress(mapUser.get("address").toString());
		userVO.setPhone(mapUser.get("phone").toString());
		userVO.setRoleid(mapUser.get("roleid").toString());
		
		
		
		userVO.setUserid(request.getParameter("backUser.userid"));
		
		managementUserForm.setBackUser(userVO);
		List<HashMap<String,String>> roles = baseDataService.getRoleData();
		
		request.setAttribute("roles", roles);
		return mapping.findForward("toAddUser");
	}
	
	
	public void getRoleAndArea(ActionForm form,
			HttpServletRequest request)
	throws Exception {
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		baseDataService = (BaseDataService) ac.getBean("baseDataService");
		List<HashMap<String,String>> areas = baseDataService.getAreaData();
		List<HashMap<String,String>> roles = baseDataService.getRoleData();
		request.setAttribute("areas", areas);
		request.setAttribute("roles", roles);
		
	}
	
	public ActionForward submitUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		BackUserVO backUserVO=managementUserForm.getBackUser();
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
		
		if(!backUserVO.getUserid().equals("")){
			maintainVehicleService.updateUser(backUserVO);
			
			managementUserForm.setBackUser(new BackUserVO());
			return this.getUser(mapping, managementUserForm, request, response);
		}
		
		
		if(maintainVehicleService.isExistUserNum(backUserVO)){
			request.setAttribute("userError", "用户名在系统中已经存在！");
			this.getRoleAndArea( managementUserForm,request);
			return mapping.findForward("toAddUser");
		}
		
		
		maintainVehicleService.submitUser(backUserVO);
		request.setAttribute("userError", "用户创建成功！");
		managementUserForm.setBackUser(new BackUserVO());
		
		this.getRoleAndArea( managementUserForm,request);
		return mapping.findForward("toAddUser");
	}
	
	public ActionForward getUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		ManagementUserForm managementUserForm= (ManagementUserForm)form;
		ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
		HashMap<String,Object> map = new HashMap<String,Object>();
		BackUserVO backUserVO=managementUserForm.getBackUser();
		
		if(backUserVO.getUsername()!=null&&!(backUserVO.getUsername().trim().equals(""))){
			map.put("username", backUserVO.getUsername());
		}else{
			map.put("username", null);
		}
		
		if(backUserVO.getUsernum()!=null&&!(backUserVO.getUsernum().trim().equals(""))){
			map.put("usernum", backUserVO.getUsernum());
		}else{
			map.put("usernum", null);
		}
		
		if(backUserVO.getRoleid()!=null&&!(backUserVO.getRoleid().trim().equals("-1"))){
			map.put("roleid", backUserVO.getRoleid());
		}else{
			map.put("roleid", null);
		}
		
		if(backUserVO.getAreaid()!=null&&!(backUserVO.getAreaid().trim().equals("-1"))){
			map.put("areaid", backUserVO.getAreaid());
		}else{
			map.put("areaid", null);
		}
		
		
		if(request.getParameter("pager.offset")==null||"".equals(request.getParameter("pager.offset"))){
			map.put("skip", 0);
			managementUserForm.setOffset("0");
		}else{
			if(new Integer(request.getParameter("pager.offset"))!=0){
				map.put("skip", new Integer(request.getParameter("pager.offset")));
				managementUserForm.setOffset(request.getParameter("pager.offset"));
			}else{
				map.put("skip", 0);
				managementUserForm.setOffset("0");
			}
			
		}
		map.put("limit", managementUserForm.getMaxCount()); 
		this.getRoleAndArea( managementUserForm,request);
		List<HashMap<String,Object>> userList =maintainVehicleService.getUser(map);
		request.setAttribute("userList", userList);
		request.setAttribute("total", maintainVehicleService.getUserCount(map));
		request.setAttribute("maxCount", managementUserForm.getMaxCount());
		request.setAttribute("url","getUser");
		return mapping.findForward("userList");
	}
	



public ActionForward getUserArea(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
throws Exception {
	ManagementUserForm managementUserForm= (ManagementUserForm)form;
	ServletContext sc = request.getSession().getServletContext();
	ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	maintainVehicleService = (MaintainVehicleService) ac.getBean("maintainVehicleService");
	HashMap<String,Object> map = new HashMap<String,Object>();
	BackUserVO backUserVO=managementUserForm.getBackUser();
	
	if(backUserVO.getUsername()!=null&&!(backUserVO.getUsername().trim().equals(""))){
		map.put("username", backUserVO.getUsername());
	}else{
		map.put("username", null);
	}
	
	if(backUserVO.getUsernum()!=null&&!(backUserVO.getUsernum().trim().equals(""))){
		map.put("usernum", backUserVO.getUsernum());
	}else{
		map.put("usernum", null);
	}
	
	if(backUserVO.getRoleid()!=null&&!(backUserVO.getRoleid().trim().equals("-1"))){
		map.put("roleid", backUserVO.getRoleid());
	}else{
		map.put("roleid", null);
	}
	
	if(backUserVO.getAreaid()!=null&&!(backUserVO.getAreaid().trim().equals("-1"))){
		map.put("areaid", backUserVO.getAreaid());
	}else{
		map.put("areaid", null);
	}
	
	
	if(request.getParameter("pager.offset")==null||"".equals(request.getParameter("pager.offset"))){
		map.put("skip", 0);
		managementUserForm.setOffset("0");
	}else{
		if(new Integer(request.getParameter("pager.offset"))!=0){
			map.put("skip", new Integer(request.getParameter("pager.offset")));
			managementUserForm.setOffset(request.getParameter("pager.offset"));
		}else{
			map.put("skip", 0);
			managementUserForm.setOffset("0");
		}
		
	}
	map.put("limit", managementUserForm.getMaxCount()); 
	this.getRoleAndArea( managementUserForm,request);
	List<HashMap<String,Object>> userList =maintainVehicleService.getUser(map);
	request.setAttribute("userList", userList);
	request.setAttribute("total", maintainVehicleService.getUserCount(map));
	request.setAttribute("maxCount", managementUserForm.getMaxCount());
	request.setAttribute("url","getUser");
	return mapping.findForward("userAreaList");
}

}
